package com.cet.algorithm.双指针法.移除元素;

import java.util.Arrays;

/**
 * @program: algorithm
 * @description: 移动零
 * @author: 陈恩涛
 * @create: 2022-03-23 21:09
 **/
public class LC283 {

    /**
     * 先将数组中的零移除，最后用慢指针补充零
     * @param nums
     */
    public void moveZeroes(int[] nums) {
        int slowIndex = 0;
        for (int fastIndex = 0; fastIndex < nums.length; fastIndex++) {
            if (nums[fastIndex] != 0) {
                nums[slowIndex] = nums[fastIndex];
                slowIndex++;
            }
        }
        while (slowIndex < nums.length) {
            nums[slowIndex++] = 0;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 0, 2, 0, 3};
        new LC283().moveZeroes(nums);
        Arrays.stream(nums).forEach(System.out::println);
    }
}
